package cst.a720yyq.zyfypt_720yyq.Modle;

import cst.a720yyq.zyfypt_720yyq.Interface.CollectListener;
import cst.a720yyq.zyfypt_720yyq.Interface.CollectService;
import cst.a720yyq.zyfypt_720yyq.Interface.Collectiface;
import cst.a720yyq.zyfypt_720yyq.Interface.ResService;
import cst.a720yyq.zyfypt_720yyq.Interface.ToudouIface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by 藤堂 on 2017/6/7.
 */

public class CollectModel implements Collectiface{
    private Retrofit retrofit;
    private String BASEURL
            ="http://amicool.neusoft.edu.cn/";
    public CollectModel()
    {   //使用Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }


    @Override
    public void collect(String mod, int id, String sessionid, final CollectListener l) {
        CollectService collectService=retrofit.create(CollectService.class);
        Call<String> call= collectService.collect(mod,id,sessionid);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {

                if (response.isSuccessful() && response != null) {
                    if (response.body().trim().toString().equals("0"))
                        l.onRecponce("1");//收藏失败
                    else if (!response.body().trim().toString().contains("error"))
                        l.onRecponce("2");//收藏成功
                    else
                        l.onRecponce("收藏：" + response.body());
                }
                else   l.onFail("collect onresponse fail");//
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail("收藏失败"+t.toString());
            }
        });

    }

    @Override
    public void uncollect(String mod, int id, String sessionid, final CollectListener l) {
        CollectService  collectService=retrofit.create(CollectService.class);
        Call<String> call = collectService.collect(mod,id,sessionid);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {  if(response.body().trim().toString().equals("0"))
                    l.onRecponce("4");//取消收藏失败
                else if (response.body().trim().toString().equals("1"))
                    l.onRecponce("5");//取消收藏成功
                else
                    l.onRecponce("取消收藏："+response.body());

                }
                else   l.onFail("uncollect onresponse fail");//uncollect onresponse fail

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail("取消收藏失败："+t.toString());
            }
        });
    }

    @Override
    public void exist(String mod, int id, String sessionid, final CollectListener l) {
        final CollectService  collectService=retrofit.create(CollectService.class);
        Call<String>call=collectService.exists(mod,id,sessionid);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {  if(response.body().trim().toString().equals("0"))//已收藏
                    l.onRecponce("7");
                else if (response.body().trim().toString().equals("1"))//未收藏
                    l.onRecponce("8");
                else
                    l.onRecponce("判断收藏："+response.body());

                }
                else   l.onFail("collect exist onresponse fail");//

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail("判断收藏失败："+t.toString());
            }
        });
    }
}
